﻿using System;
using System.Reflection;
using MessagePack.Formatters;
using MessagePack.Internal;

namespace MessagePack.Resolvers
{
	// Token: 0x02000110 RID: 272
	public class DynamicContractlessObjectResolver : IFormatterResolver
	{
		// Token: 0x060003D3 RID: 979 RVA: 0x0000E800 File Offset: 0x0000CC00
		private DynamicContractlessObjectResolver()
		{
		}

		// Token: 0x060003D5 RID: 981 RVA: 0x0000E823 File Offset: 0x0000CC23
		public IMessagePackFormatter<T> GetFormatter<T>()
		{
			return DynamicContractlessObjectResolver.FormatterCache<T>.formatter;
		}

		// Token: 0x0400016E RID: 366
		public static readonly DynamicContractlessObjectResolver Instance = new DynamicContractlessObjectResolver();

		// Token: 0x0400016F RID: 367
		private const string ModuleName = "MessagePack.Resolvers.DynamicContractlessObjectResolver";

		// Token: 0x04000170 RID: 368
		private static readonly DynamicAssembly assembly = new DynamicAssembly("MessagePack.Resolvers.DynamicContractlessObjectResolver");

		// Token: 0x02000111 RID: 273
		private static class FormatterCache<T>
		{
			// Token: 0x060003D6 RID: 982 RVA: 0x0000E82C File Offset: 0x0000CC2C
			static FormatterCache()
			{
				TypeInfo_wzh typeInfo = typeof(T).GetTypeInfo_wzh();
				if (typeInfo.IsNullable())
				{
					typeInfo = typeInfo.GenericTypeArguments[0].GetTypeInfo_wzh();
					object formatterDynamic = DynamicObjectResolver.Instance.GetFormatterDynamic(typeInfo.AsType());
					if (formatterDynamic == null)
					{
						return;
					}
					DynamicContractlessObjectResolver.FormatterCache<T>.formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(new Type[]
					{
						typeInfo.AsType()
					}), new object[]
					{
						formatterDynamic
					});
					return;
				}
				else
				{
					if (!typeof(T).GetTypeInfo_wzh().IsPublic() && !typeof(T).GetTypeInfo_wzh().IsNestedPublic && typeInfo.IsClass)
					{
						DynamicContractlessObjectResolver.FormatterCache<T>.formatter = (IMessagePackFormatter<T>)DynamicPrivateFormatterBuilder.BuildFormatter(typeof(T));
						return;
					}
					TypeInfo_wzh typeInfo2 = DynamicObjectTypeBuilder.BuildType(DynamicContractlessObjectResolver.assembly, typeof(T), true);
					if (typeInfo2 == null)
					{
						return;
					}
					DynamicContractlessObjectResolver.FormatterCache<T>.formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeInfo2.AsType());
					return;
				}
			}

			// Token: 0x04000171 RID: 369
			public static readonly IMessagePackFormatter<T> formatter;
		}
	}
}
